var Bmob = require('../../dist/Bmob-1.6.0.min.js');
var app = getApp()
const {
  $Message
} = require('../../dist/base/index');
Page({
  data: {

    colorArr: ["blue", "green", "red", "pink", "yellow"],
    randomColor: [],
    current: '3',
    current_scroll: '3',

    array: [],
    contentarray: [
      {
        title: "有没有人来学习啊",
        extra: "this is extra",
        name: "_abu", imageurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        content: "what do you want? \n 大量收款按时看案例asldk啊实打实飒飒的空间奥斯卡了撒肯德基阿斯利康加快老实交代了看见家里跨世纪的刻录机 ?",
        tag1: "学习",
        tag2: "自习"

      }, {
        title: "this is one",
        extra: "this is extra",
        name: "_abu",
        imageurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        content: "what do you want? \n 大量收款按时看案例asldk啊实打实飒飒的空间奥斯卡了撒肯德基阿斯利康加快老实交代了看见家里跨世纪的刻录机 ?",
        tag1: "学习",
        tag2: "自习"

      }, {
        title: "this is one",
        extra: "this is extra",
        name: "_abu",
        imageurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        content: "<p> what do you want?</p>< p > 大量收款按时看案例asldk啊实打实飒飒的空间奥斯卡了撒肯德基阿斯利康加快老实交代了看见家里跨世纪的刻录机 ?",
        tag1: "学习",
        tag2: "自习"

      }, {
        title: "this is one",
        extra: "this is extra",
        name: "_abu",
        imageurl: "http://npic7.edushi.com/cn/zixun/zh-chs/2018-06/15/9a9ce41b2071431ab76a1cf1bdeb345c.jpg",
        content: "<p> what do you want?</p>< p > 大量收款按时看案例asldk啊实打实飒飒的空间奥斯卡了撒肯德基阿斯利康加快老实交代了看见家里跨世纪的刻录机 ?",
        tag1: "学习",
        tag2: "自习"

      }



    ]
  },
  getLabelColor: function () {
    let that = this
    var colorLen = this.data.colorArr.length;
    var arrLen = this.data.array.length;
    var colorArr = this.data.colorArr;
    var newArr = [];
    var newcolorArr = [];
    var pre = null;
    for (var i = 0; i < arrLen; i++) {
      newcolorArr = [];
      for (var j = 0; j < that.data.array[i].tag.length; j++) {
        while (random == pre) {
          var random = colorArr[Math.floor(Math.random() * colorLen)];
        }
        pre = random;
        newcolorArr.push(random);
      }

      newArr.push(newcolorArr);
    }
    this.setData({
      randomColor: newArr
    })
  },
  onLoad: function (options) {
    let that = this
    console.log('index.js:onLoad')

    wx.setNavigationBarTitle({
      title: '我的发表'
    });
    //如果登陆成功则跳出登陆成功的提示~
    if (app.globalData.isLogin && !app.globalData.isLoginShowed) {
      $Message({
        content: '登陆成功',
        type: 'success'
      });
      app.globalData.isLoginShowed = true
    }

    //把颜色随机好待用
    that.getLabelColor()

    //去数据库把帖子都拿出来 顺便把帖子的作者拿出来
    var list = []
    var temp = {}
    //查找帖子数据
    const query = Bmob.Query("Postings");
    query.include('user', 'post')
    // query.descending("createdAt")
    query.order('-createdAt');
    query.statTo("where", '{"user":{"$inQuery":{"where":{"objectId":"' + app.globalData.authData.objectId + '"},"className":"_User"}}}');
    query.find().then(res => {
      for (let i = 0; i < res.length; i++) {
        var label_list;
        //查找tags
        const query1 = Bmob.Query("label")
        query1.statTo("where", '{"$relatedTo":{"object":{"__type":"Pointer","className":"Postings","objectId":"' + res[i].objectId + '"},"key":"tag"}}');
        // query1.statTo("where", '{"tag":{"$inQuery":{"where":{"tag":"乒乓球"},"className":"label"}}}');
        query1.find().then(res1 => {
          //查找UserData
          const pointer = Bmob.Pointer('_User')
          const poiID = pointer.set(res[i].user.objectId)

          const query = Bmob.Query('UserData')
          query.include('major', 'post')
          query.equalTo("user", "==", poiID);
          query.find().then(res2 => {
            label_list = []
            for (var j = 0; j < res1.length; j++) {
              label_list.push(res1[j].tag);
            }
            //把帖子的数据存入list
            list = that.data.array
            if (res2.length) {
              //如果服务器有UserData
              temp = {
                objectid: res[i].objectId,
                title: res[i].title,
                user: res[i].user,
                name: res2[0].nickName,
                imageurl: res2[0].userPic.url,
                content: res[i].detail,
                tag: label_list
              }
            }
            else {
              temp = {
                //如果服务器没有UserData
                objectid: res[i].objectId,
                title: res[i].title,
                user: res[i].user,
                name: res[i].user.nickName,
                imageurl: res[i].user.userPic,
                content: res[i].detail,
                tag: label_list
              }

            }
            list.push(temp)
            that.setData({
              array: list
            })
            that.getLabelColor()
          })
          // console.log(that.data.array)
        }).catch(err => {
          console.log(err)
        })
      }
    }).catch(err => {
      $Message({
        content: '获取服务器信息失败',
        type: 'error'
      });
      console.log(err)
    })




    //累死了不想写注释了
  },
  detailPage: function (e) {
    console.log(e.currentTarget.id)
    //这个大概是点了一下那个详情
    wx.navigateTo({
      url: '../../pages/detail/detail?objectId=' + e.currentTarget.id
    })
  },
  userPage: function (e) {
    console.log(e.currentTarget.id)
    //这个是点了头像或者昵称那行
    wx.navigateTo({
      url: '../../pages/idcard/idcard?objectId=' + e.currentTarget.id
    })
  },
  handleChange({ detail }) {
    this.setData({
      current: detail.key,
    });
    console.log('handleChange', this.data);
  },

  handleChangeScroll({ detail }) {
    this.setData({
      current_scroll: detail.key
    });
    console.log('handleChangeScroll', detail.key);
  }

});